package com.ccys.cms.controller.productAndService;


import com.ccys.cms.service.productAndService.ProductIngredientService;
import com.ccys.cms.service.productAndService.ProductService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.web.productAndService.BatchIngredientDto;
import com.ccys.db.dto.web.productAndService.NumEnum;
import com.ccys.db.dto.web.productAndService.ProductDto;
import com.ccys.db.entity.TblProduct;
import com.ccys.db.entity.TblProductIngredient;
import com.ccys.db.mapper.TblProductMapper;
import com.ccys.db.vo.web.productAndService.GetProductListVo;
import com.ccys.db.vo.web.productAndService.ProductTypeVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/productAndService")
@Api(tags = "产品管理")
public class ProductController {


    @Resource
    private ProductService service;

    @Resource
    private ProductIngredientService ingredientService;


    @Resource
    private TblProductMapper mapper;


    @PostMapping("product")
    @ApiOperation("新增")
    @Transactional
    public ResponseResult insert(@RequestBody BatchIngredientDto model) {

       return  service.change(model);
    }


    @PutMapping("product")
    @ApiOperation("修改")
    @Transactional
    public ResponseResult update(@RequestBody BatchIngredientDto model) {

        return  service.change(model);
    }

    @PutMapping("product/status")
    @ApiOperation("修改药品状态、单价、排序")
    @Transactional
    public ResponseResult update(TblProduct product) {
        TblProduct model = new TblProduct();
        model.setSellingPricePerUnit(product.getSellingPricePerUnit());
        model.setId(product.getId());
        model.setDispOrder(product.getDispOrder());
        model.setStatus(product.getStatus());
        int i = service.insertOrUpdate(model);
        return  ResponseResult.update(i);
    }


    @GetMapping("product/detail")
    @ApiOperation("根据id查询")
    public ResponseResult<BatchIngredientDto, Object> getById(String id) {

        TblProduct entityById = service.findEntityById(id);
        //根据id查询Ingredient
        List<TblProductIngredient> ingredientList = ingredientService.findListByLimit(TblProductIngredient.class, new ArrayList<String>() {
            {
                add(" product_id = '" + id + "'");
            }
        }, "created_on_utc desc");
        BatchIngredientDto res = new BatchIngredientDto();
        BeanUtils.copyProperties(entityById, res);
        res.setIngredientList(ingredientList);
        return ResponseResult.success(res);
    }


    @DeleteMapping("product")
    @ApiOperation("根据Id删除")
    @Transactional
    public ResponseResult delete(String id) {

        TblProduct model = new TblProduct();
        model.setId(id);
        model.setDelFlag(NumEnum.DEL.getCode());
        int i = service.updateEntityById(model);
        //应删除之前对应的IngredientList
        service.deleteIngredients(id);
        return ResponseResult.update(i);
    }


    @GetMapping("product")
    @ApiOperation(value = "查询分页", notes = "排序字段 t1.name/ t5.group_name / t6.name(指的是国家名称) / t1.price / t1.disp_order  / t1.status /t2. user_fullname / t1.created_on ")
    public ResponseResult<PageInfo<ProductTypeVo>, Object> getPage(ProductDto dto) {

        return service.getPage(dto);
    }


    @GetMapping("product/list")
    @ApiOperation("根据groupId查询list")
    public ResponseResult<List<GetProductListVo>,Object> getList(String groupId){
        List<GetProductListVo> productList = mapper.getProductList(groupId);
        return ResponseResult.success(productList);
    }




}
